<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use DB;

class Company extends Model
{

    protected $table = 'company';

    public $timestamps = false;

    protected $fillable = ['schedule'];

    //获取基础管理类
	public function getGroup()
	{
		return $this->belongsTo('App\Model\Checkgroup','category');
	}
    
    public function userinfo(){
        return $this->hasOne('App\Model\Users','companyId','id');
    }
    public function tagrelations(){
        return $this->hasMany('App\Model\TagRelation','com_id','id');
    }
    // 住户详情
    public function resident(){
        return $this->hasOne('App\Model\Resident','com_id','id');
    }
    //
    public function safeOfficer()
    {
    	return $this->hasMany('App\Model\Comsafeofficer','companyId');
    }
    public function commanager()
    {
        return $this->hasMany('App\Model\Commanager','companyId');
    }
    public function comcheck(){
        return $this->hasOne('App\Model\Comcheck','companyId','id');
    }
    public function place_1()
    {
        return $this->belongsTo('App\Model\Area','province');
    }
    public function place_2()
    {
        return $this->belongsTo('App\Model\Area','city');
    }
    public function place_3()
    {
        return $this->belongsTo('App\Model\Area','district');
    }
    public function place_4()
    {
        return $this->belongsTo('App\Model\Area','street');
    }
    public function place_5()
    {
        return $this->belongsTo('App\Model\Area','community');
    }
    public function place_11()
    {
        return $this->belongsTo('App\Model\Area','province1');
    }
    public function place_22()
    {
        return $this->belongsTo('App\Model\Area','city1');
    }
    public function place_33()
    {
        return $this->belongsTo('App\Model\Area','district1');
    }
    public function place_44()
    {
        return $this->belongsTo('App\Model\Area','street1');
    }
    public function place_55()
    {
        return $this->belongsTo('App\Model\Area','community1');
    }
    public function healthOfficer()
    {
    	return $this->hasMany('App\Model\Comhealthofficer','companyId');
    }
    public function meet()
    {
        return $this->hasMany('App\Model\Meet','company_id');
    }
    //所属单位分类
    public function checkcategory()
    {
        return $this->belongsTo('App\Model\Checkcategory','categoryId');
    }

    public function getAlidata()
    {
        $date = date('Y-m-d');
        $rs = DB::select(
            "select company.* from company left join comcheck cck on company.id = cck.companyId 
            where cck.created > '{$date}'"
        );
        return $rs;

    }
    public function stocks(){
        return $this->hasMany('App\Model\Stock','company_id','id');
    }
    /*获取公司信息*/
    public function getCompanyList($name,$category)
    {
        $where = "1=1";
        if(!empty($name)) {
            $where = $where.' and company.comName like "%'.$name.'%"';
        }
        if(!empty($category)) {
            $where = $where.' and company.categoryId in ('.$category.')';
        }
        $where = $where.' and company.lng != ""';
        $rs = DB::select("select company.id,company.comName,company.lng,company.lat,comcheck.id as checkId,company.enterprise from 
                        company left join comcheck on company.id = comcheck.companyId 
                        where ".$where);
        return $rs;
    }

    public function getCompanyNumByCommunity()
    {
        $sql = "select count(id) as num,community from company group by community";
        return DB::select($sql);
    }
}
